//
//  TileItem.m
//  ElongClient
//
//  Created by  on 14/12/1.
//  Copyright (c) 2014年 elong. All rights reserved.
//

#import "TileItem.h"
#define TAG_SHADOW_VIEW 3001//点击的阴影view的tag

@implementation TileItem
-(id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        self.iconImg = _iconImg;
        self.titleStr = _titleStr;
        self.tipsBtn = _tipsBtn;
        self.markNum = _markNum;
        self.delegate = _delegate;
        self.numLabel = _numLabel;
    }
    return self;
}
-(void)layoutSubviews{
        //self增加手势
        if (!tap) {
            tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
            [self addGestureRecognizer:tap];
        }
        
        //imageView
        if (!imageView) {
            imageView = [[UIImageView alloc]initWithImage:self.iconImg];
            imageView.frame = CGRectMake(self.frame.size.width*0.5f-self.iconImg.size.width*0.5f, 23, self.iconImg.size.width, self.iconImg.size.width);
            [self addSubview:imageView];
        }
        
        //titleLabel
        if (!titleLabel) {
            titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, imageView.frame.origin.y+imageView.frame.size.height+20, 0, 0)];
            titleLabel.backgroundColor = [UIColor clearColor];
            titleLabel.font = [UIFont systemFontOfSize:13.0];
            titleLabel.textColor = RGBCOLOR(52.0, 52.0, 52.0, 1.0);
            titleLabel.text = self.titleStr;
            
            [self setVerticalCenterOfLabel:titleLabel];//label居中对齐
            [self addSubview:titleLabel];
        }        }

//label居中对齐
-(void)setVerticalCenterOfLabel:(UILabel *)label{
    CGSize size = [label.text sizeWithFont:label.font];
    CGRect rect = label.frame;
    rect.origin.x = self.frame.size.width * 0.5f - size.width * 0.5f;
    rect.size = size;
    label.frame = rect;
}

-(void)tap{
    if ([self.delegate respondsToSelector:@selector(tileItemAction:)]) {
        [self.delegate tileItemAction:self];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //添加点击效果
    UIView * shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width-SCREEN_SCALE, self.frame.size.height-SCREEN_SCALE)];
    shadowView.backgroundColor = RGBCOLOR(237.0, 237.0, 237.0, 1.0);
    shadowView.tag = TAG_SHADOW_VIEW;
    [self addSubview:shadowView];
    [self bringSubviewToFront:imageView];
    [self bringSubviewToFront:titleLabel];
    [self bringSubviewToFront:_numLabel];
    
}
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    //取消九宫格点中效果
    UIView * view = [self viewWithTag:TAG_SHADOW_VIEW];
    if (view) {
        [view removeFromSuperview];
    }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView * view = [self viewWithTag:TAG_SHADOW_VIEW];
    if (view) {
        [view removeFromSuperview];
    }
}

@end
